package com.example.robertluozizhao.framecollectdemo.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.robertluozizhao.framecollectdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by luozizhao on 2018/7/2.
 */

@SuppressLint("LongLogTag")
public class ChildThreadCommunicaActivity extends AppCompatActivity {
    @BindView(R.id.btn_start01)
    Button btnStart;
    @BindView(R.id.btn_start02)
    Button btnStart02;
    private static final String TAG = "ChildThreadCommunicaActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child_thread_communication);
        ButterKnife.bind(this);
    }


    private void start01() {
        HandlerThread headlerThread = new HandlerThread("headlerThread");
        headlerThread.start();

        final Handler handler = new Handler(headlerThread.getLooper()) {

            @Override
            public void handleMessage(Message msg) {
                String mes = null;
                String name = Thread.currentThread().getName();
                Log.d(TAG, "执行线程："+name);
                if (msg.what == 1) {
                    mes = (String) msg.obj;
                }
                Log.d(TAG, "headlerThread接收数据为：" + mes);
            }
        };


        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG,"执行线程："+Thread.currentThread().getName());
                Message obtain = Message.obtain();
                obtain.what = 1;
                obtain.obj = "发送 headlerThread 消息";
                handler.sendMessage(obtain);
            }
        }).start();
    }


    private void start02() {

    }

    @OnClick({R.id.btn_start01, R.id.btn_start02})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start01:
                start01();
                break;
            case R.id.btn_start02:
                start02();
                break;
        }
    }
}
